<template>
	<view>
		<view class="usermain">
			<view class="usermain-item ">
				<view>技师头像</view>

				<view @click="uploadImg()">
					<image src="../../static/logo.png" v-if="artificerImg==null" mode=""
						style="width: 111rpx;height: 111rpx;border-radius: 50%;"></image>
					<image v-else :src="artificerImg" mode="" style="width: 111rpx;height: 111rpx;border-radius: 50%;">
					</image>
				</view>
			</view>
			<view class="usermain-item item-padding ">
				<view>技师名称</view>
				<view>
					<view class="cu-form-group">
						<input type="text" v-model="artificerName" placeholder="请输入技师名称" />
					</view>
				</view>
			</view>
			<!-- <view class="usermain-item item-padding ">
				<view>技师手机</view>
				<view class="cu-form-group">
					<input v-model="phone" disabled placeholder="请输入技师手机" />
				</view>
			</view> -->
			<view class="usermain-item item-padding " v-if="city">
				<view>区域</view>
				<view class="cu-form-group">
					<input v-model="city" @click="openMap" />
				</view>
			</view>
			<view class="usermain-item item-padding " v-if="rate">
				<view>佣金</view>
				<view class="cu-form-group">
					<input v-model="rate" />
				</view>
			</view>
			<view class="usermain-item item-padding ">
				<view>项目类型</view>
				<view @click="show = true">
					<view class="cu-form-group">
						<u-radio-group v-model="classifyId">
							<u-radio shape="circle" active-color="#2FB57A" :name="classifyId">{{classifyName}}</u-radio>
						</u-radio-group>
					</view>
				</view>
			</view>
			<view class="usermain-item item-padding ">
				<view>是否优选</view>
				<view>
					<view class="cu-form-group">
						<u-radio-group v-model="isGoods">
							<u-radio shape="circle" active-color="#2FB57A" :name="1">是</u-radio>
							<u-radio shape="circle" active-color="#2FB57A" :name="2">否</u-radio>
						</u-radio-group>
					</view>
				</view>
			</view>
			<view class="usermain-item item-padding ">
				<view>是否热度最高</view>
				<view>
					<view class="cu-form-group">
						<u-radio-group v-model="isHot">
							<u-radio shape="circle" active-color="#2FB57A" :name="1">是</u-radio>
							<u-radio shape="circle" active-color="#2FB57A" :name="2">否</u-radio>
						</u-radio-group>
					</view>
				</view>
			</view>

			<view class="usermain-item item-padding ">
				<view>出行方式</view>
				<view>
					<view class="cu-form-group">
						<u-radio-group v-model="tripWay">
							<u-radio shape="circle" active-color="#2FB57A" :name="1">公交</u-radio>
							<u-radio shape="circle" active-color="#2FB57A" :name="2">出租</u-radio>
							<u-radio shape="circle" active-color="#2FB57A" :name="3">免费</u-radio>
						</u-radio-group>
					</view>
				</view>
			</view>
			<view class="padding-lr margin-top">
				<view class="">技师简介</view>
				<view class="margin-tb-sm">
					<u-input v-model="content" type="textarea" height="200" placeholde="请描述一下您的特长" maxlengt="200"
						:clearable="false" />
				</view>
				<view class=" margin-top-sm">资质证书</view>
				<view class="flex" style="overflow: hidden;flex-wrap: wrap;">
					<view class="margin-top flex margin-right-sm flex-wrap" v-if="certificates.length">
						<view class="flex" style="width: 200rpx;height: 200rpx;margin-right: 2rpx;position: relative;"
							v-for="(image,index) in certificates" :key="index">
							<image :src="image" style="width: 100%;height: 100%;"></image>
							<view style="z-index: 9;position: absolute;top: -15rpx;right: -15rpx;"
								@click="removeImg(index,1)">
								<u-icon name="close-circle-fill" color="#2FB57A" size="50rpx"></u-icon>
							</view>
						</view>
					</view>
					<view class="margin-top" @click="addImages(1)" v-if="certificates.length<6">
						<view style="width: 200rpx;height: 200rpx;background: #f4f5f6;"
							class="flex justify-center align-center">
							<view>
								<view class="text-center">
									<image src="../../static/images/my/add.png" style="width: 65rpx;height: 55rpx;">
									</image>
								</view>
								<view class="text-center">添加图片</view>
							</view>
						</view>
					</view>
				</view>
				<view class=" margin-top-sm">生活照</view>
				<view class="flex" style="overflow: hidden;flex-wrap: wrap;">
					<view class="margin-top flex margin-right-sm flex-wrap" v-if="lifePhotos.length">
						<view class="flex" style="width: 200rpx;height: 200rpx;margin-right: 2rpx;position: relative;"
							v-for="(image,index) in lifePhotos" :key="index">
							<image :src="image" style="width: 100%;height: 100%;"></image>
							<view style="z-index: 9;position: absolute;top: -15rpx;right: -15rpx;"
								@click="removeImg(index,2)">
								<u-icon name="close-circle-fill" color="#2FB57A" size="50rpx"></u-icon>
							</view>
						</view>
					</view>
					<view class="margin-top" @click="addImages(2)" v-if="lifePhotos.length<6">
						<view style="width: 200rpx;height: 200rpx;background: #f4f5f6;"
							class="flex justify-center align-center">
							<view>
								<view class="text-center">
									<image src="../../static/images/my/add.png" style="width: 65rpx;height: 55rpx;">
									</image>
								</view>
								<view class="text-center">添加图片</view>
							</view>
						</view>
					</view>
				</view>
			</view>

		</view>
		<view class="footer-btn">
			<view class="usermain-btn" @click="messagebtn()">保存</view>
		</view>


		<u-popup v-model="show" mode="center" border-radius="14" width="500rpx" height="170px">
			<view class="padding">
				<view class="text-center text-lg text-bold">项目类型</view>
				<view class="margin-top-sm">
					<u-radio-group v-model="classifyName" @change="radioGroupChange">
						<u-radio @change="radioChange(item)" v-for="(item, index) in list" :key="index"
							:name="item.value" :id="item.id" active-color="#2FB57A" :disabled="item.disabled">
							{{item.value}}
						</u-radio>
					</u-radio-group>
				</view>

				<view class="btn" @click="show = false">确定</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import configdata from '../../common/config.js';
	export default {
		data() {
			return {
				phone: '',
				artificerImg: '../../static/logo.png',
				artificerName: '',
				city: '',
				rate: '',
				classifyName: '',
				classifyId: '',
				list: [],
				show: false,
				isGoods: 1,
				isHot: 1,
				tripWay: '',
				lifePhoto: '',
				certificate: '',
				lifePhotos: [], //生活照
				certificates: [], //资质证书
				content: '',

				artificerId: ''
			};
		},
		onLoad(e) {
			if (e.artificerId) {
				this.artificerId = e.artificerId
			}
			this.getUserInfo()
			this.getType()
			// this.avatar = uni.getStorageSync('avatar')
		},
		methods: {
			removeImg(index,ind) {
				if(ind==1){
					this.certificates.splice(index, 1)
				}else if(ind==2){
					this.lifePhotos.splice(index, 1)
				}
				
			},
			// 选中某个单选框时，由radio时触发
			radioChange(e) {
				this.classifyName = e.classifyName
				this.classifyId = e.classifyId
				console.log(e);
			},
			// 选中任一radio时，由radio-group触发
			radioGroupChange(e) {
				// console.log(e);
			},
			getType() {
				this.$Request.get('/app/dict/list?type=服务类型&status=1').then(res => {
					if (res.code == 0) {
						res.data.forEach(d => {
							d.disabled = false
						})
						this.list = res.data
						console.log(this.list)
					}
				});
			},
			openMap() {
				let that = this
				uni.chooseLocation({
					success: function(res) {
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
						that.latitude = res.latitude
						that.longitude = res.longitude
						that.getcity(res.latitude, res.longitude)
					}
				});
			},
			getcity(latitude, longitude) {
				this.$Request.get('/app/Login/selectCity?lat=' + latitude + '&lng=' + longitude).then(res => {
					if (res.code == 0) {
						this.city = res.data.city ? res.data.city : '未知'
					}
				});
			},
			// goMyAddress() {
			// 	uni.navigateTo({
			// 		url: '../jifen/myaddress'
			// 	});
			// },
			uploadImg() {
				let token = uni.getStorageSync('token')

				if (!token) {
					this.goLoginInfo();
					return;
				}
				let that = this;
				var url = null;
				uni.showActionSheet({
					// itemList按钮的文字接受的是数组
					itemList: ["查看头像", "从相册选择图片"],
					success(e) {
						var index = e.tapIndex
						if (index === 0) {
							// 用户点击了预览当前图片
							// 可以自己实现当前头像链接的读取
							let url = that.artificerImg;
							let arr = []
							arr.push(url)
							uni.previewImage({
								// 预览功能图片也必须是数组的
								urls: arr
							})
						} else if (index === 1) {
							uni.chooseImage({
								count: 1, //默认9
								sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
								sourceType: ['album'], //从相册选择
								success: function(res) {
									uni.showLoading({
										title: '上传中...'
									});
									let token = uni.getStorageSync('token');
									let userId = uni.getStorageSync('userId');
									uni.uploadFile({
										url: 'https://anmomerchant7.0.xianmaxiong.com/sqx_fast/alioss/upload', //真实的接口地址

										filePath: res.tempFilePaths[0],
										header: {
											token: token
										},
										name: 'file',
										success: uploadFileRes => {
											url = JSON.parse(uploadFileRes.data);
											that.artificerImg = url.data
											uni.hideLoading();
										}
									});
								}
							});
						}
					}
				})
			},
			config: function(name) {
				var info = null;
				if (name) {
					var name2 = name.split("."); //字符分割
					if (name2.length > 1) {
						info = configdata[name2[0]][name2[1]] || null;
					} else {
						info = configdata[name] || null;
					}
					if (info == null) {
						let web_config = cache.get("web_config");
						if (web_config) {
							if (name2.length > 1) {
								info = web_config[name2[0]][name2[1]] || null;
							} else {
								info = web_config[name] || null;
							}
						}
					}
				}
				return info;
			},
			getUserInfo() {
				let userId = uni.getStorageSync('userId')
				let data = {
					artificerId: this.artificerId
				}
				this.$Request.get("/app/artificer/selectArtificerById", data).then(res => {
					if (res.code == 0) {
						this.artificerImg = res.data.artificerImg
						this.artificerName = res.data.artificerName;
						this.phone = res.data.phone;
						this.city = res.data.city
						this.rate = res.data.rate
						this.classifyName = res.data.classifyName
						this.classifyId = res.data.classifyId
						this.isGoods = res.data.isGoods ? res.data.isGoods : '0'
						this.isHot = res.data.isHot ? res.data.isHot : '0'
						this.tripWay = res.data.tripWay ? res.data.tripWay : '0'
						this.content = res.data.content
						//资质证书
						if (res.data.certificate) {
							this.certificates = res.data.certificate.split(',')
							this.certificate = res.data.certificate
						}
						//生活照
						if (res.data.lifePhoto) {
							this.lifePhotos = res.data.lifePhoto.split(',')
							this.lifePhoto = res.data.lifePhoto
						}

					}
					uni.hideLoading();
				});


			},
			// 保存
			messagebtn() {
				this.certificate = this.certificates
				this.certificate = this.certificate.toString();

				this.lifePhoto = this.lifePhotos
				this.lifePhoto = this.lifePhoto.toString();
				uni.showModal({
					title: '温馨提示',
					content: '确定保存信息',
					success: e => {
						if (e.confirm) {
							this.$Request.postJson("/app/artificer/updateArtificers", {
								artificerImg: this.artificerImg,
								artificerName: this.artificerName,
								city: this.city,
								rate: this.rate,
								classifyName: this.classifyName,
								classifyId: this.classifyId,
								isGoods: this.isGoods,
								isHot: this.isHot,
								tripWay: this.tripWay,
								lifePhoto: this.lifePhoto,
								certificate: this.certificate,
								content: this.content,
								artificerId:this.artificerId
							}).then(res => {
								if (res.code === 0) {
									uni.showToast({
										title: '保存成功',
										icon: "none"
									})
									setTimeout(function() {
										uni.navigateBack()
									}, 1000)
								} else {
									uni.showToast({
										title: res.msg,
										icon: "none"
									})
								}
							})
						}
					}
				});
			},
			addImages(e) {
				let that = this
				let urlName = 'https://anmomerchant7.0.xianmaxiong.com/sqx_fast/alioss/upload';
				if (e == 1) {
					urlName = 'https://anmomerchant7.0.xianmaxiong.com/sqx_fast/alioss/uploadWatermark';
				}
				uni.chooseImage({
					count: 6,
					sourceType: ['album', 'camera'],
					success: res => {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							that.$queue.showLoading("上传中...");
							uni.uploadFile({ // 上传接口
								// url: this.config("APIHOST1") + '/alioss/upload', //真实的接口地址
								url: urlName,
								filePath: res.tempFilePaths[i],
								name: 'file',
								success: (uploadFileRes) => {
									if (e == 1) {
										if (that.certificates.length <= 5) {
											that.certificates.push(JSON.parse(uploadFileRes
													.data)
												.data)
										}
									} else if (e == 2) {
										if (that.lifePhotos.length <= 5) {
											that.lifePhotos.push(JSON.parse(uploadFileRes
													.data)
												.data)
										}
									}

									uni.hideLoading();
								}
							});
						}
					}
				})
			},
			addImage(e) {
				let that = this
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'],
					success: res => {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							that.$queue.showLoading("上传中...");
							uni.uploadFile({ // 上传接口
								// url: this.config("APIHOST1") + '/alioss/upload', //真实的接口地址
								url: 'https://anmomerchant7.0.xianmaxiong.com/sqx_fast/alioss/upload',
								filePath: res.tempFilePaths[i],
								name: 'file',
								success: (uploadFileRes) => {
									if (e == 1) {
										that.form.companyImage = JSON.parse(uploadFileRes.data)
											.data
									} else if (e == 4) {
										that.form.companyLogo = JSON.parse(uploadFileRes.data).data
										console.log(that.form.companyLogo)
									}
									uni.hideLoading();

								}
							});
						}
					}
				})
			},
			config: function(name) {
				var info = null;
				if (name) {
					var name2 = name.split("."); //字符分割
					if (name2.length > 1) {
						info = configdata[name2[0]][name2[1]] || null;
					} else {
						info = configdata[name] || null;
					}
					if (info == null) {
						let web_config = cache.get("web_config");
						if (web_config) {
							if (name2.length > 1) {
								info = web_config[name2[0]][name2[1]] || null;
							} else {
								info = web_config[name] || null;
							}
						}
					}
				}
				return info;
			}
		}
	};
</script>

<style>
	page {
		/* background: #1c1b20; */
	}

	.usermain {
		background: #ffffff;
		/* color: #fff; */
	}

	.usermain-item {
		display: flex;
		align-items: center;
		margin: 0 40rpx;
		padding: 10rpx 0;
		justify-content: space-between;
		border-bottom: 1rpx solid #e5e5e5;
		/* border-bottom: 2rpx solid #f2f2f2; */
	}

	.usermain-item.item-padding {
		/* padding: 0; */
	}

	.cu-form-group {
		padding: 0;
		background: #ffffff;
		text-align: right;
	}

	.cu-form-group input {
		background: #ffffff;
		font-size: 28rpx;
		/* color: #fff; */

	}

	.footer-btn {
		margin-top: 90rpx;
		padding-bottom: 80rpx;
	}

	.footer-btn .usermain-btn {
		color: #FFFFFF;
		background: #2FB57A;
		text-align: center;
		width: 450rpx;
		height: 80rpx;
		font-size: 28rpx;
		line-height: 80rpx;
		margin: 0 auto;
		border-radius: 40rpx;
	}

	.btn {
		color: #FFFFFF;
		background: #2FB57A;
		text-align: center;
		border-radius: 16rpx;
		padding: 20rpx 0;
		margin-top: 80rpx;
	}
</style>
